package com.daocao.hua.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.daocao.hua.R;
import com.daocao.hua.api.Api;
import com.daocao.hua.constant.UserGlobal;
import com.daocao.hua.util.Network;
import com.daocao.hua.util.RxLifeUtil;
import com.daocao.hua.util.ToastUtil;
import com.daocao.hua.util.Util;
import com.daocao.hua.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3BankListActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.img_bank_bg)
    ImageView imgBankBg;
    @BindView(R.id.btn_add)
    SuperButton btnAdd;
    @BindView(R.id.layout_without_bank)
    RelativeLayout layoutWithoutBank;
    @BindView(R.id.img_bank_icon)
    ImageView imgBankIcon;
    @BindView(R.id.fl_bank_icon)
    FrameLayout flBankIcon;
    @BindView(R.id.txt_bank_name)
    TextView txtBankName;
    @BindView(R.id.txt_bank_type)
    TextView txtBankType;
    @BindView(R.id.txt_bank_num)
    TextView txtBankNum;
    @BindView(R.id.txt_phone)
    TextView txtPhone;
    @BindView(R.id.layout_has_bank)
    LinearLayout layoutHasBank;
    @BindView(R.id.btn_change_bank)
    SuperButton btnChangeBank;
    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_list_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("添加银行卡");
    }

    @Override
    protected void onResume() {
        super.onResume();
        getBankList();
    }

    private void getBankList() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }

        Api.api_service.getBankList().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {

                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (!TextUtils.isEmpty(result.getReal_name())) {
                        UserGlobal.setRealName(result.getReal_name());
                    }
                    if (result.getBanks() == null || result.getBanks().size() <= 0) {
                        layoutWithoutBank.setVisibility(View.VISIBLE);
                        layoutHasBank.setVisibility(View.GONE);
                        return;
                    }
                    layoutWithoutBank.setVisibility(View.GONE);
                    layoutHasBank.setVisibility(View.VISIBLE);
                    txtPhone.setText(UserGlobal.getMobile());
                    txtBankName.setText(result.getBanks().get(0).getName());
                    txtBankNum.setText(result.getBanks().get(0).getBankcard());
                    txtBankType.setText(result.getBanks().get(0).getCardtype());
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick({R.id.btn_change_bank, R.id.btn_back, R.id.btn_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_change_bank:
                Util.startActivity(this, V3BankEditActivity.class);
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_add:
                Util.startActivity(this, V3BankEditActivity.class);
                break;
        }
    }
}
